package com.sxfq.loansupermarket.backsystem.modular.marketing.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.SearchApprovalProcessInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ApprovalProcessInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.IApprovalProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 审核信息
 *
 * @author
 * @Date
 */
@Controller
@RequestMapping("/approvalProcess")
public class ApprovalProcessController extends BaseController {
    @Autowired
    IApprovalProcessService service;

    private String PREFIX = "/returnVisit/";

    /**
     * 跳转到审核界面
     */
    @RequestMapping("")
    public String reservations(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("channelAuditLevel", ShiroKit.getUser().getChannelAuditLevel());
        return PREFIX + "approval_process.html";
    }

    /**
     * 审核管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object ApprovalProcessList(SearchApprovalProcessInfo info, HttpServletRequest request) {
        Page<ApprovalProcessInfo> page = new PageFactory<ApprovalProcessInfo>().defaultPage();
        try {
            List<ApprovalProcessInfo> list = new ArrayList<ApprovalProcessInfo>();
            if("exe".equals(info.getExeValue())){
                info.setStartIndex(page.getSize()*(page.getCurrent()-1));
                info.setLimitIndex(page.getSize());
                page.setCurrent(0);
                list = service.selectPageListByGetMap(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = service.selectPageListByGetMap(page, info);
                }
                page.setTotal(service.getQueryPagesGetMapCount(page, info));
                page.setRecords(list);
            }else if("tri".equals(info.getExeValue())){
                info.setStartIndex(page.getSize()*(page.getCurrent()-1));
                info.setLimitIndex(page.getSize());
                page.setCurrent(0);
                list = service.selectPageListByGetMapYi(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = service.selectPageListByGetMapYi(page, info);
                }
                page.setTotal(service.getQueryPagesGetMapCountYi(page, info));
                page.setRecords(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }
}